package com.ruibang.glass.material.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruibang.glass.material.domain.resp.external.*;
import com.ruibang.glass.material.dto.CpBcpLotDTO;
import com.ruibang.glass.material.entity.MaterialContentLot;
import com.ruibang.glass.material.entity.MaterialLotItem;
import com.ruibang.glass.material.mapper.MaterialLotItemMapper;
import com.ruibang.glass.material.service.MaterialLotItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 容器中玻璃绑定状态信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-02-05
 */
@Service
public class MaterialLotItemServiceImpl extends ServiceImpl<MaterialLotItemMapper, MaterialLotItem> implements MaterialLotItemService {

    @Override
    public List<MaterialLotItem> queryGlassBindByParams(String bindId){
        return baseMapper.queryGlassBindByParams(bindId);
    }

    @Override
    public int getCountByBindId(String bindId){
        return baseMapper.getCountByBindId(bindId);
    }

    @Override
    public List<ContentTypeStartTime> getLotItemTypeStartTime(){
        return baseMapper.getLotItemTypeStartTime();
    }

    /***
     * 同步冷端玻璃绑定数据
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void syncColdSideGLassData(String startTime){
        baseMapper.updateCodeLotItem(startTime);
        List<RackBindDetail> records = baseMapper.querySLRackBindDetail(startTime);
        if(CollectionUtils.isNotEmpty(records)){
            List<MaterialLotItem> lotItems = new ArrayList<>();
                    records.stream().forEach(r->{
                        if(r!=null){
                            MaterialLotItem item = new MaterialLotItem();
                            item.setItemId(r.getId());
                            item.setContentLotId(r.getRackUseBatch());
                            item.setBindId(r.getBindRecordId());
                            item.setGlassCode(r.getGlassId());
                            item.setGlassSerialNumber(StringUtils.isEmpty(r.getGlassOrderNumber())?null:Integer.parseInt(r.getGlassOrderNumber()));
                            item.setStatus(r.getBindStatus());
                            item.setBindTime(r.getBindTime());
                            item.setUnbindTime(r.getUnbindTime());
                            item.setCreateTime(r.getCreateTime());
                            lotItems.add(item);
                        }
                    });
            System.out.println("冷端插入lotItem表数据条数");
            baseMapper.insertBatch(lotItems,10000);
        }
    }

    /***
     * 同步热端玻璃绑定数据
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void syncHotSideGLassData(String startTime){
        baseMapper.updateHotLotItem(startTime);
        List<RackBindDetail> records = baseMapper.queryBCRackBindDetail(startTime);
        if(CollectionUtils.isNotEmpty(records)){
            List<MaterialLotItem> lotItems = new ArrayList<>();
                    records.stream().forEach(r->{
                        if(r!=null){
                            MaterialLotItem item = new MaterialLotItem();
                            item.setItemId(r.getId());
                            item.setContentLotId(r.getRackUseBatch());
                            item.setBindId(r.getBindRecordId());
                            item.setGlassCode(r.getGlassId());
                            item.setGlassSerialNumber(StringUtils.isEmpty(r.getGlassOrderNumber())?null:Integer.parseInt(r.getGlassOrderNumber()));
                            item.setStatus(r.getBindStatus());
                            item.setBindTime(r.getBindTime());
                            item.setUnbindTime(r.getUnbindTime());
                            item.setCreateTime(r.getCreateTime());
                            lotItems.add(item);
                        }
                    });
            baseMapper.insertBatch(lotItems,10000);
        }
    }

    @Override
    public List<CpBcpLotDTO> getcontentLotIdAndCount() {
        return baseMapper.getContentLotIdAndCount();
    }

}
